// pages/shop/address/address.js
var app = getApp();
import Api from '../../../api/api.js';
const api = new Api();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isCartEmpty:false,
    addressList:[],
    isNeedFresh: true,
    sourceType:'',

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.sourceType = options.type;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.isNeedFresh){
      this.getAddressList();
    }
  },
  getAddressList() {
    var _this = this;
    var map = {};
    map.appId = app.APP_ID;
    map.userId = app.USER_ID();;
    api.getRequestData(app.mineAdressList, map, false).then(res => {
      if (res.data.errorCode == 0) {
        var list = res.data.model;
        _this.setData({
          addressList:list
        });
        if (list.length){
          _this.setData({
            isCartEmpty: false
          });
        } else {
          _this.setData({
            isCartEmpty:true
          });
        }
      } else {
        app.alert(res.data.errorMsg);
      }
    });
  },
  addAdress(){
    wx.navigateTo({
      url: '/pages/mine/address/add/add',
    })
  },
  editAddress(e){
    var index = parseInt(e.currentTarget.dataset.key + "");
    var dic = this.data.addressList[index];
    wx.setStorageSync("addressDic", JSON.stringify(dic));
    wx.navigateTo({
      url: '/pages/mine/address/edit/edit',
    })
  },
  itemClick(e){
    var index = parseInt(e.currentTarget.dataset.key + "");
    var dic = this.data.addressList[index];
    if (this.data.sourceType == "selectAdd"){
      var page = getCurrentPages();
      var upPage = page[page.length - 2];
      app.settlementPageNeedFresh = true;
      upPage.setData({
        isHaveAddress:true,
        adressDict: dic
      });
      wx.navigateBack();
    }

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.data.isNeedFresh = false;
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})